package la.iok.hzsvn.lewin.movie.shopmovie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.model.UserVo;
import la.iok.hzsvn.lewin.movie.core.shopmovie.vo.PlayerFeeVo;
import la.iok.hzsvn.lewin.movie.shopmovie.model.*;
import la.iok.hzsvn.lewin.movie.shopmovie.service.CarpoolService;
import la.iok.hzsvn.lewin.movie.shopmovie.service.CouponUseRecordService;
import la.iok.hzsvn.lewin.mybatis.model.PageParam;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.lewin.mybatis.model.SortParam;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("carpools")
public class CarpoolController {
    private final CarpoolService carpoolService;
    private final CouponUseRecordService couponUseRecordService;

    public CarpoolController(CarpoolService carpoolService, CouponUseRecordService couponUseRecordService) {
        this.carpoolService = carpoolService;
        this.couponUseRecordService = couponUseRecordService;
    }

    @PostMapping
    public SingleDataResponse<Long> save(@RequestHeader Token token, @RequestBody CarpoolParam param){
        return new SingleDataResponse<>(carpoolService.save(token, param));
    }

    @PutMapping("{id}")
    public OperatorResponse modify(@RequestHeader Token token, @PathVariable Long id, @RequestBody CarpoolParam param){
        carpoolService.modify(token, id, param);
        return OperatorResponses.success();
    }

    @GetMapping("{id}")
    public SingleDataResponse<CarpoolVo> load(@PathVariable Long id){
        return new SingleDataResponse<>(carpoolService.po2vo(carpoolService.select(id)));
    }

    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token, @PathVariable Long id) {
        carpoolService.delete(token, id);
        return OperatorResponses.success();
    }

    @GetMapping
    public SingleDataResponse<PageVo<CarpoolVo>> page(@RequestHeader Token token, PageParam pageParam,
                                                      SortParam sortParam, CarpoolListParam listParam){
        return new SingleDataResponse<>(carpoolService.page(token, pageParam, sortParam, listParam));
    }

    /**
     * 拉玩家到拼车中
     */
    @PostMapping("{id}/players")
    public ListDataResponse<JoinPlayerResult> joinPlayer(@RequestHeader Token token, @PathVariable Long id, @RequestBody JoinPlayerParam param){
        return new ListDataResponse<>(carpoolService.joinPlayer(token, id, param));
    }

    @GetMapping("{id}/players")
    public ListDataResponse<PlayerVo> players(@RequestHeader Token token, @PathVariable Long id) {
        return new ListDataResponse<>(carpoolService.players(token, id));
    }

    /**
     * 添加拼车的收费项.这里是添加附加收费项，不包含剧本费
     * @return 操作结果
     */
    @PostMapping("{id}/fees")
    public OperatorResponse fees(@RequestHeader Token token, @PathVariable Long id, @RequestBody PlayerFeesParam param){
        carpoolService.fees(token, id, param);
        return OperatorResponses.success();
    }

    @GetMapping("{id}/fees")
    public ListDataResponse<PlayerFeeVo> playerFees(@RequestHeader Token token, @PathVariable Long id){
        return new ListDataResponse<>(couponUseRecordService.fees(token, id));
    }

    @PutMapping("{id}/start")
    public OperatorResponse start(@RequestHeader Token token, @PathVariable Long id){
        carpoolService.start(token, id);
        return OperatorResponses.success();
    }

    @PutMapping("{id}/end")
    public OperatorResponse end(@RequestHeader Token token, @PathVariable Long id){
        carpoolService.end(token, id);
        return OperatorResponses.success();
    }

    @PutMapping("{id}/confirmor")
    public OperatorResponse confirm(@RequestHeader Token token, @PathVariable Long id) {
        carpoolService.confirm(token, id);
        return OperatorResponses.success();
    }

    @PostMapping("dm/report")
    public SingleDataResponse<CarpoolReport> dmCarpoolReport(@RequestHeader Token token, @RequestBody CarpoolReport report) {
        return new SingleDataResponse<>(carpoolService.dmCarpoolReport(token, report));
    }

    @GetMapping("recordId/{userId}")
    public SingleDataResponse<Long> selectRecordId(@RequestHeader Token token,@PathVariable Long userId)
    {
        return new SingleDataResponse<>(carpoolService.selectRecordId(userId));
    }

}
